﻿#region SYSTEM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Net;
using System.Text.RegularExpressions;
using System.Diagnostics;

#endregion
using DevExpress.XtraEditors.DXErrorProvider;

using Common;
using Common.DbConstants;
using BusinessEntity.Patient;
using BusinessEntity.Common;
using DataAccess.Common;


#region Microsoft
using Microsoft.ApplicationBlocks.Data;
#endregion

namespace DataAccess.Provider
{
    public class ProviderAdapter
    {
        public static SortableCollection<PatientVisit> SearchVisits(string sessionId, BusinessEntity.Patient.Patient oSearchPatient, out string successMessage, out string errorMessage)
        {
            IDataReader dataReader = null;
            PatientVisit oVisit = null;

            errorMessage = string.Empty;
            successMessage = string.Empty;

            SortableCollection<PatientVisit> oVisits = new SortableCollection<PatientVisit>();
            try
            {
                SqlParameter outputSuccessMessage = DbUtility.GetSuccessMessageParameter();
                SqlParameter outputErrorMessage = DbUtility.GetErrorMessageParameter();


                dataReader = SqlHelper.ExecuteReader(
                                             Utility.GetApplicationConnectionString(),
                                             CommandType.StoredProcedure,
                                             DbStoredProcedures.PATIENT_SEARCHVISITS,
                                              new SqlParameter(DbParameters.SESSIONID, sessionId),
                                              new SqlParameter(DbParameters.PATIENTID, oSearchPatient.Id),
                                              outputSuccessMessage,
                                              outputErrorMessage
                                             );

                while (dataReader.Read())
                {
                    oVisit = new PatientVisit();
                    oVisit.Id = Utility.GetDataReaderIntegerFieldValue(dataReader, DbColumnNames.ID);
                    oVisit.VisitStatus = dataReader[DbColumnNameVisit.VISITSTATUSNAME].ToString();
                    oVisit.VisitStatusId = (VisitStatusType)Utility.GetDataReaderIntegerFieldValue(dataReader, DbColumnNameVisit.VISITSTATUSID);
                    oVisit.IsActive = Convert.ToBoolean(dataReader[DbColumnNameVisit.VISITISACTIVE].ToString());
                    PatientAppointment oPA = new PatientAppointment();
                    oPA.StartDate = Convert.ToDateTime(dataReader["AppointmentStartDate"].ToString());
                    oVisit.PatientAppointment = oPA;
                    oVisit.Log = CommonAdapter.SetCreateUpdatedLog(dataReader);
                    oVisits.Add(oVisit);
                }

                dataReader.Close();

                if (outputSuccessMessage.Value != null)
                {
                    successMessage = outputSuccessMessage.Value.ToString();
                }

                if (outputErrorMessage.Value != null)
                {
                    errorMessage = outputErrorMessage.Value.ToString();
                }
                return oVisits;
            }
            catch (Exception ex)
            {
                errorMessage = errorMessage + Environment.NewLine + Utility.GetExceptionMessageString(ex);
                return null;
            }
            finally
            {
                if (dataReader != null)
                {
                    if (!dataReader.IsClosed)
                    {
                        dataReader.Close();
                    }
                }
            }
        }

        public static SortableCollection<PatientVisit> AddVisit(string sessionId, BusinessEntity.Patient.Patient oPatient, out string successMessage, out string errorMessage)
        {
            IDataReader dataReader = null;
            PatientVisit oVisit = null;

            errorMessage = string.Empty;
            successMessage = string.Empty;

            SortableCollection<PatientVisit> oVisits = new SortableCollection<PatientVisit>();
            try
            {
                SqlParameter outputSuccessMessage = DbUtility.GetSuccessMessageParameter();
                SqlParameter outputErrorMessage = DbUtility.GetErrorMessageParameter();


                dataReader = SqlHelper.ExecuteReader(
                                             Utility.GetApplicationConnectionString(),
                                             CommandType.StoredProcedure,
                                             DbStoredProcedures.PATIENT_ADDVISITS,
                                              new SqlParameter(DbParameters.SESSIONID, sessionId),
                                              new SqlParameter(DbParameters.PATIENTID, oPatient.Id),
                                              new SqlParameter("@VisitDate", oPatient.PatientVisit[0].PatientAppointment.StartDate),
                                              outputSuccessMessage,
                                              outputErrorMessage
                                             );

               
                dataReader.Close();

                if (outputSuccessMessage.Value != null)
                {
                    successMessage = outputSuccessMessage.Value.ToString();
                }

                if (outputErrorMessage.Value != null)
                {
                    errorMessage = outputErrorMessage.Value.ToString();
                }
                return oVisits;
            }
            catch (Exception ex)
            {
                errorMessage = errorMessage + Environment.NewLine + Utility.GetExceptionMessageString(ex);
                return null;
            }
            finally
            {
                if (dataReader != null)
                {
                    if (!dataReader.IsClosed)
                    {
                        dataReader.Close();
                    }
                }
            }
        }
    }
}
